package cn.hotel.controller.worker;

import cn.hotel.entity.Cleaner;
import cn.hotel.response.ResponseTool;
import cn.hotel.response.Result;
import cn.hotel.service.CleanerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.relational.core.sql.In;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping(value = "/op/cleaner")
public class OpCleanerController {

    @Resource
    private CleanerService cleanerService;

    @PostMapping("")
    public Result getAllCleaner() {
        List<Cleaner> allCleaner = cleanerService.getAllCleaner();
        log.info("查询到的cleaner信息{}" , allCleaner);

        if (allCleaner == null) {
            return ResponseTool.failed("查询失败");
        }
        return ResponseTool.success(allCleaner);
    }

    @PostMapping("delete/{cleanerId}")
    public Result delById(@PathVariable Integer cleanerId){
        log.info("获取的cleanerId的信息{}" , cleanerId);
        int i = cleanerService.delById(cleanerId);

        if (i == 0) {
            return ResponseTool.failed("删除失败");
        }
        return ResponseTool.success();
    }

    @PostMapping("/deleteS")
    public Result delByIds(@RequestParam Integer[] cleaners) {
        log.info("获取users的信息{}" , cleaners);
        if (cleaners == null) {
            ResponseTool.failed("查询失败!!");
        }
        for (int i = 0; i < cleaners.length; i++) {
            System.out.println(cleaners[i]);
            cleanerService.delById(cleaners[i]);
        }

        return ResponseTool.success();

    }

    @PostMapping("/add")
    public Result addCleaner(Cleaner cleaner) {
        log.info("获取的cleaner的信息{}" , cleaner);
        int i = cleanerService.addCleaner(cleaner);

        if (i == 0){
            return ResponseTool.failed("添加失败");
        }
        return ResponseTool.success();
    }

    @PostMapping("/update")
    public Result updateCleaner(Cleaner cleaner) {
        log.info("获取的cleaner的信息{}" , cleaner);
        int i = cleanerService.updateCleaner(cleaner);

        if (i ==0) {
            return ResponseTool.failed("更新失败");
        }
        return ResponseTool.success();
    }

    @PostMapping("/{cleanerId}")
    public Result getCleanerById(@PathVariable Integer cleanerId) {
        log.info("获取的cleanerId的信息{}" , cleanerId);
        Cleaner byId = cleanerService.getById(cleanerId);

        if (byId == null) {
            return ResponseTool.failed("查询失败");
        }
        return ResponseTool.success(byId);
    }

}
